﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WCrawlerLib;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace WCrawler.Controls
{
    public partial class ucSearchHostFiles : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtUsername.Text = "hgtuan2003";
            txtPassword.Text = "$4819830112078$";
            txtEmail.Text = "hgtuan2003@yahoo.com";
        }

        public DataRow SearchFileServe(DataRow dr)
        {
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);
            BrowserSession_FileHost br = new BrowserSession_FileHost();
            //==============================================================================================================================================
            //Login
            br.PostParams = string.Format("loginUserName={0}&loginUserPassword={1}&autoLogin=on&ppp=102&loginFormSubmit=Login", username, password);
            br.Post("http://fileserve.com/login.php", false);
            System.Net.CookieCollection cookieCollect = br.Response.Cookies;

            //==============================================================================================================================================
            //Search Folder Name
            br.PostParams = string.Format("fileNameSearch={0}&fileOrFolderSearch=folder&fileSizeSearch=&upperFileSizeSearch=&lowerFileSizeSearch=&fileTypeSearch=&startDateSearch=&endDateSearch=&fileSearchFormSubmit=Search&fileSearchCurrentFolderId=0", txtFolderName.Text);
            br.SaveCookiesFrom(cookieCollect);
            br.Post("http://fileserve.com/file-manager.php");
            HtmlAgilityPack.HtmlNode node = br.HtmlDoc.DocumentNode.SelectSingleNode(string.Format(".//a[text()='{0}']",txtFolderName.Text));
            string folderId = node.Attributes["id"].Value;

            //==============================================================================================================================================
            //Search file list in folder
            //br.PostParams = string.Format("extra=folderChild&parent_folder_id={0}", folderId);
            br.PostParams = string.Format("parent_folder_id={0}&total_folder_count=0&current_page=1&sort_field=2&sort_order=DESC", folderId);
            br.SaveCookiesFrom(cookieCollect);
            br.Post("http://fileserve.com/file-manager-expand-folder.php");

            //==============================================================================================================================================
            dr[0] = "FileServe";
            string pattern = @"downloadLink:(?<href>.+)";
            Regex r = new Regex(pattern);
            string content = HttpUtility.HtmlDecode(br.HtmlDoc.DocumentNode.InnerHtml).Replace("\"", "").Replace("\\","");
            var query = from p in content.Split(new char[] { ',' }).AsQueryable()
                        where p.Contains("downloadLink:")
                        select p;
            string hostfiles = "";
            foreach (string s in query)
            {
                Match m = r.Match(s);
                if (m.Success){
                    string value = m.Result("${href}").Replace(@"<br/>", "");
                    string[] arrStr = value.Split(new char[]{'/'});
                    string title = arrStr[arrStr.Length - 1];
                    string href = value.Replace(title, "");
                    hostfiles += string.Format("<a href=\"{0}\">{1}</a><br/>", href, title);
                }
            }

            
            dr[1] = hostfiles;

            return dr;
        }

        public DataSet CreateDataSet()
        {
            DataSet ds = new DataSet();
            DataTable db = new DataTable("Forums");
            db.Columns.Add("Host");
            db.Columns.Add("Files");
            ds.Tables.Add(db);
            return ds;
        }

        public DataRow SearchFileSonic(DataRow dr)
        {
            string folderId = "D605169";
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);
            System.Net.CookieCollection cookieCollect;
            BrowserSession_FileHost br = new BrowserSession_FileHost();

            //==============================================================================================================================================
            //Login
            br.PostParams = string.Format("email={0}&redirect=%2F&password={1}", username, password);
            br.Post("http://www.filesonic.vn/user/login", false);
            cookieCollect = br.Response.Cookies;
            if (br.HtmlDoc.DocumentNode.InnerHtml.Contains("Username") &&
                br.HtmlDoc.DocumentNode.InnerHtml.Contains("Password"))
            {
                txtFolderName.Text = "Login Fail";
                return null;
            }

            ////==============================================================================================================================================
            ////Get Folder List
            //br.PostParams = string.Format("http://www.filesonic.vn/filesystem/browse", txtFolderName.Text);
            //br.SaveCookiesFrom(cookieCollect);
            //br.Post("http://www.filesonic.vn/filesystem/browse ");
            //HtmlAgilityPack.HtmlNode node = br.HtmlDoc.DocumentNode.SelectSingleNode(".//*[@id='folderTree']//a[@title = 'Movies']");
            //string url = node.Attributes["href"].Value;

            //==============================================================================================================================================
            //Search folder url
            br.SaveCookiesFrom(cookieCollect);
            string url = string.Format("http://www.filesonic.vn/filesystem/browse/{0}?partial=folderTree&orderFoldersBy=name&orderFoldersDirection=asc", folderId);
            br.Get(url);
            HtmlAgilityPack.HtmlNodeCollection nodeCollect = br.HtmlDoc.DocumentNode.SelectNodes(".//*[@id='mainContent']//a");
            url = "";
            foreach (HtmlAgilityPack.HtmlNode nod in nodeCollect)
            {
                if (nod.Attributes["title"].Value.Contains(txtFolderName.Text))
                {
                    url = nod.Attributes["href"].Value;
                    break;
                }
            }

            //==============================================================================================================================================
            //Search file list in folder
            br.SaveCookiesFrom(cookieCollect);
            br.Get(url);
            nodeCollect = br.HtmlDoc.DocumentNode.SelectNodes(".//*[@id='fileList']/ul/li");
            url = "";
            string hostfiles = "";
            foreach (HtmlAgilityPack.HtmlNode nod in nodeCollect)
            {
                string title = nod.SelectSingleNode("./a").Attributes["title"].Value;
                string href = string.Format("http://www.filesonic.com/file/{0}", nod.SelectSingleNode("./input").Attributes["rel"].Value);
                hostfiles += string.Format("<a href=\"{0}\">{1}</a><br/>", href, title);
            }
            dr[0] = "FileSonic";
            dr[1] = hostfiles;
            return dr;
        }

        public DataRow SearchFileFactory(DataRow dr)
        {
            string folderId = "";
            //string email = HttpUtility.UrlEncode(txtEmail.Text);
            string username = HttpUtility.UrlEncode(txtEmail.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);
            System.Net.CookieCollection cookieCollect;
            BrowserSession_FileHost br = new BrowserSession_FileHost();

            //==============================================================================================================================================
            //Login
            br.PostParams = string.Format("redirect=%2F&email={0}&password={1}", username, password);
            br.Post("http://filefactory.com/member/login.php", false);
            cookieCollect = br.Response.Cookies;
            //if (br.HtmlDoc.DocumentNode.InnerHtml.Contains("Username") &&
            //    br.HtmlDoc.DocumentNode.InnerHtml.Contains("Password"))
            //{
            //    txtFolderName.Text = "Login Fail";
            //    return null;
            //}
        
            //==============================================================================================================================================
            //Search folder url
            br.SaveCookiesFrom(cookieCollect);
            string url = "http://www.filefactory.com/manager/get.php?m=getFolders";
            br.Get(url);
            string[] sArray = br.HtmlDoc.DocumentNode.InnerHtml.Split(new string[] {"{attributes:"}, StringSplitOptions.None);
            var query = from p in sArray.AsQueryable()
                            where p.Contains(txtFolderName.Text)
                            select p;
            
            foreach (string s in query)
            {
                Match m = Regex.Match(s,"id:\"(?<folderId>\\w+)\"");
                if (m.Success)
                {
                    folderId = m.Result("${folderId}");
                }
            }

            if (string.IsNullOrEmpty(folderId))
                return dr;

            //==============================================================================================================================================
            //Search file list in folder
            br.SaveCookiesFrom(cookieCollect);
            url = string.Format("http://www.filefactory.com/manager/get.php?_={0}&m=getFiles&f={1}&p=1&so=DESC&ss=sortCreated", DateTime.Now.Ticks.ToString(), folderId);
            br.Get(url);
            string hostfiles = "";
            Regex r = new Regex("name:\"(?<name>(.*?))\"(.*?)url:\"(?<url>(.*?))\"");
            MatchCollection mCollect = r.Matches(br.HtmlDoc.DocumentNode.InnerHtml);
            if (mCollect.Count > 0)
            {
                foreach (Match m in mCollect)
                {
                    string title = m.Result("${name}");
                    string href = m.Result("${url}");
                    hostfiles += string.Format("<a href=\"{0}\">{1}</a><br/>", href, title);
                }
            }
            dr[0] = "FileFactory";
            dr[1] = hostfiles;
            return dr;
        }

        public DataRow SearchDepositFiles(DataRow dr)
        {
            string folderId = "";
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);
            System.Net.CookieCollection cookieCollect;
            BrowserSession_FileHost br = new BrowserSession_FileHost();

            //==============================================================================================================================================
            //Login
            br.PostParams = string.Format("go=1&login={0}&password={1}", username, password);
            br.Post("http://depositfiles.com/login.php?return=%2F", false);
            cookieCollect = br.Response.Cookies;
            //if (br.HtmlDoc.DocumentNode.InnerHtml.Contains("Username") &&
            //    br.HtmlDoc.DocumentNode.InnerHtml.Contains("Password"))
            //{
            //    txtFolderName.Text = "Login Fail";
            //    return null;
            //}

            //==============================================================================================================================================
            //Search folder url
            br.SaveCookiesFrom(cookieCollect);
            string url = "http://depositfiles.com/gold/files_list.php?format=json&action=list_folders";
            br.Get(url);
            string[] sArray = br.HtmlDoc.DocumentNode.InnerHtml.Split(new char[] {'{'});
            var query = from p in sArray.AsQueryable()
                        where p.Contains(txtFolderName.Text)
                        select p;

            foreach (string s in query)
            {
                Match m = Regex.Match(s, "id\":\"(?<folderId>\\d+)\"");
                if (m.Success)
                {
                    folderId = m.Result("${folderId}");
                }
            }

            if (string.IsNullOrEmpty(folderId))
                return dr;
            //==============================================================================================================================================
            //Search file list in folder
            br.SaveCookiesFrom(cookieCollect);
            url = string.Format("http://depositfiles.com/gold/files_list.php?format=json&s[id_file_folder]={0}", folderId);
            br.Get(url);
            string hostfiles = "";
            string pattern = "filename_source\":\"(?<name>(.*?))\"(.*?)download_url\":\"(?<url>(.*?))\"";
            MatchCollection mCollect = Regex.Matches(br.HtmlDoc.DocumentNode.InnerHtml.Replace("\n",""), pattern);
            if (mCollect.Count > 0)
            {
                foreach (Match m in mCollect)
                {
                    string title = m.Result("${name}");
                    string href = m.Result("${url}");
                    hostfiles += string.Format("<a href=\"{0}\">{1}</a><br/>", href, title);
                }
            }
            dr[0] = "Depositfiles";
            dr[1] = hostfiles;
            return dr;
        }

        public void SearchHostFiles_DataBind()
        {
            if (Session["PostId"] != null)
            {
                Post objPost = new Post();
                objPost = objPost.GetPost((int)Session["PostId"]);
                txtFolderName.Text = objPost.HostFolder;
            }
        }

        protected void btnSearchFiles_Click(object sender, EventArgs e)
        {
            DataSet ds = CreateDataSet();
            DataRow dr;

            //Filefactory
            dr = ds.Tables[0].NewRow();
            dr = SearchFileFactory(dr);
            ds.Tables[0].Rows.Add(dr);

            //Depositfiles
            dr = ds.Tables[0].NewRow();
            dr = SearchDepositFiles(dr);
            ds.Tables[0].Rows.Add(dr);

            ////Fileserve
            //dr = ds.Tables[0].NewRow();
            //dr = SearchFileServe(dr);
            //ds.Tables[0].Rows.Add(dr);
            
            ////Filesonic
            //dr = ds.Tables[0].NewRow();
            //dr = SearchFileSonic(dr);
            //ds.Tables[0].Rows.Add(dr);

            //binding
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
}